package com.sigerap.comensal.model.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.sigerap.comanda.model.Comanda;
import com.sigerap.comensal.model.Comensal;
import com.sigerap.comensal.model.dao.ComensalDAO;
import com.sigerap.common.model.dao.EntidadDAO;

/**
 * 
 * @author Spirit-20121
 * @version 1.0
 *
 */

public class JPAComensalDAO
implements ComensalDAO{
	
	private EntityManager em;
	
	public JPAComensalDAO(EntityManager em){
		this.em=em;
	}

	@Override
	public List<Comensal> findAll() {
		// TODO Auto-generated method stub
		String jpql="Select o From Comensal o";
		Query query=em.createQuery(jpql);
		return query.getResultList();
	}

	@Override
	public Comensal insert(Comensal t) {
		// TODO Auto-generated method stub
		em.persist(t);
		return t;
	}

	@Override
	public void update(Comensal t) {
		// TODO Auto-generated method stub
		em.merge(t);
	}

	@Override
	public void delete(Comensal t) {
		// TODO Auto-generated method stub
		Comensal remove= findById(t);
		em.remove(remove);
	}

	@Override
	public Comensal findById(Comensal t) {
		Query query = em.createQuery("Select c From Comensal c where c.codComensal="+t.getCodComensal()+"");
		return (Comensal) query.getSingleResult();
	}

	@Override
	public List<Comensal> findAll(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public Comensal findByDni(String dni){
		Comensal comensal= (Comensal) em.createQuery("Select c from Comensal c where c.dni =?1").setParameter(1, dni).getSingleResult();
		
		
		return comensal;
		
	}



}
